package org.xiaov.lunasql.engine.function;

import com.alibaba.druid.sql.ast.SQLCurrentTimeExpr;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.SQLReplaceable;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * CurDate2SysDateFunc
 * </p>
 *
 * @author lzq
 * @since 2024/12/13 11:06
 */
@Slf4j
public class CurDate2SysDateFunc implements MySqlFunction {
    private final static String METHOD_NAME = "curdate";
    private final static String TARGET_METHOD_NAME = "SYSDATE";

    @Override
    public boolean accept(SQLMethodInvokeExpr methodInvokeExpr) {
        return METHOD_NAME.equalsIgnoreCase(methodInvokeExpr.getMethodName());
    }

    @Override
    public void transform(SQLMethodInvokeExpr methodInvokeExpr) {
        SQLObject parent = methodInvokeExpr.getParent();
        if (parent instanceof SQLReplaceable replaceable) {
            //函数中的当前时间，修改父节点中的参数
            SQLCurrentTimeExpr currentDateExpr = new SQLCurrentTimeExpr(SQLCurrentTimeExpr.Type.CURRENT_DATE);
            replaceable.replace(methodInvokeExpr, currentDateExpr);
        }
    }
}
